﻿using Microsoft.EntityFrameworkCore;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.Services
{
    public class RBACUserService : BaseService, IRBACUserService 
    {
        public RBACUserService(DefaultDbContext context) : base(context)
        {
        }

        public async Task<UserDto> Login(UserLoginDto userLoginDto)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.Account == userLoginDto.Account && u.Password == userLoginDto.Password);
            if (user == null)
            {
                throw new Exception("Invalid username or password.");
            }
            var first = await _context.Users.FirstOrDefaultAsync(s=>s.Account == userLoginDto.Account);
            if (first == null)
            {
                throw new Exception("用户不存在");
            }
            return new UserDto
            {
                Id = user.Id,
                Name = user.Name,
                Account = user.Account,
                Phone = user.Phone,
                Remark = user.Remark
            };
        }

        public async Task Delete(long id)
        {
            var selfId = AppSession.User!.Id;
            if (selfId == id)
            {
                throw new Exception("不能删除自己");
            }
            var user = _context.Users.FirstOrDefault(u => u.Id == id);
            _context.Users.Remove(user!);
            await _context.SaveChangesAsync();
        }

        public async Task Save(UserRegisterDto input)
        {
            var isExist = _context.Users.Any(m => m.Account == input.Account && m.Id != input.Id);
            if (isExist)
            {
                throw new Exception("已存在此账号");
            }

            if (input.Id == 0)
            {
                User user = new User()
                {
                    Id = input.Id,
                    Account = input.Account,
                    Name = input.Name,
                    Password = "123456",
                    Phone = input.Phone,
                    Remark = input.Remark,
                };
                _context.Users.Add(user);
                await _context.SaveChangesAsync();

                input.Id = user.Id;
            }
            else
            {
                var oldUser = _context.Users.FirstOrDefault(m => m.Id == input.Id);
                if (oldUser == null)
                {
                    throw new Exception("员工不存在");
                }
                oldUser.Account = input.Account;
                oldUser.Name = input.Name;
                oldUser.Phone = input.Phone;
                oldUser.Remark = input.Remark;
                _context.Users.Update(oldUser);
                await _context.SaveChangesAsync();
            }
        }
        public async Task<List<UserDto>> GetListAsync(string keyword)
        {
            if (!string.IsNullOrEmpty(keyword))
            {
                var list = await _context
                    .Users
                    .AsNoTracking()
                    .Where(m => m.Name.Contains(keyword) || m.Account.Contains(keyword))
                    .ToListAsync();
                return list.Select(s => new UserDto
                {
                    Id = s.Id,
                    Name = s.Name,
                    Account = s.Account,
                    Phone = s.Phone,
                    Remark = s.Remark
                }).ToList();
            }
            else
            {
                var list = await _context.Users.ToListAsync();
                return list.Select(s => new UserDto
                {
                    Id = s.Id,
                    Name = s.Name,
                    Account = s.Account,
                    Phone = s.Phone,
                    Remark = s.Remark
                }).ToList();
            }
        }

        public async Task<UserDto?> Get(long id)
        {
            var first = await _context.Users.Where(u => u.Id == id).FirstOrDefaultAsync();
            if (first != null)
            {
                return new UserDto()
                {
                    Id = first!.Id,
                    Name = first.Name,
                    Account = first.Account,
                    Phone = first.Phone,
                    Remark = first.Remark
                };
            }
            return null;
        }

        public async Task<UserRegisterDto?> GetRegister(long id)
        {
            var first = await _context.Users.Where(u => u.Id == id).FirstOrDefaultAsync();
            if (first != null)
            {
                return new UserRegisterDto()
                {
                    Id = first!.Id,
                    Name = first.Name,
                    Account = first.Account,
                    Password = first.Password,
                    Phone = first.Phone,
                    Remark = first.Remark
                };
            }
            return null;
        }
    }
}
